//app.js
App({
    d: {
        //userId: 0,
        userId: 0,
        appId: "",
        appKey: "",
        // ceshiUrl: 'http://127.0.0.1/siweishi_api/index.php',
        ceshiUrl: 'https://gzleren.com/miniwankeshubhsc/index.php',
        share_userid: '',
    },
    onLaunch: function () {
        //调用API从本地缓存中获取数据
        var logs = wx.getStorageSync('logs') || []
        logs.unshift(Date.now())
        wx.setStorageSync('logs', logs)
        
        // wx.getLocation({
        //   type: 'wgs84',
        //   success: function (res) {
        //     console.log(res)
        //   }
        // })
        
    },
    // onLoad: function (options){
    //   var share_userId = wx.getStorageSync('share_userId') || ''
    //   console.log(share_userId + '.28')
    //   this.d.share_userid = share_userId
    //   console.log(share_userId)
    // },

getUserInfo: function (cb) {
    var that = this
    var share_userId = wx.getStorageSync('share_userId') || ''
    console.log(share_userId + '.36')
    this.d.share_userid = share_userId
    console.log(that.d.share_userid + '.38');
    if (this.globalData.userInfo) {
        typeof cb == "function" && cb(this.globalData.userInfo)
    } else {
        //调用登录接口
        wx.login({
            success: function (res) {
              var code = res.code;
              //get wx user simple info
              wx.getUserInfo({
                success: function (res) {
                    that.globalData.userInfo = res.userInfo
                    typeof cb == "function" && cb(that.globalData.userInfo);
                    //get user sessionKey
                    //get sessionKey
                    that.getUserSessionKey(code);
                }
              });
            }
        });
//      wx.getUserInfo({
//        withCredentials: false,
//        success: function (res) {
//          that.globalData.userInfo = res.userInfo
//          typeof cb == "function" && cb(that.globalData.userInfo)
//        }
//      })
    }
},
getUserSessionKey: function (code) {
    //用户的登录状态
    var that = this;
    wx.request({
        url: that.d.ceshiUrl + '/Api/Login/getsessionkey',
        method: 'post',
        data: {
            code: code
        },
        header: {
            'Content-Type': 'application/x-www-form-urlencoded'
        },
        success: function (res) {
            //--init data        
            var data = res.data;
            if (data.status == 0) {
                wx.showToast({
                    title: data.err,
                    duration: 2000
                });
                return false;
            }

            that.globalData.userInfo['sessionId'] = data.session_key;
            that.globalData.userInfo['openid'] = data.openid;
            that.onLoginUser();
        },
        fail: function (e) {
            wx.showToast({
                title: '网络异常！err:getsessionkeys',
                duration: 2000
            });
        },
    });
},
onLoginUser: function () {
    var that = this;
    var user = that.globalData.userInfo;
    wx.request({
        url: that.d.ceshiUrl + '/Api/Login/authlogin',
        method: 'post',
        data: {
            SessionId: user.sessionId,
            gender: user.gender,
            NickName: user.nickName,
            HeadUrl: user.avatarUrl,
            openid: user.openid,
            share_userId: that.d.share_userid
        },
        header: {
            'Content-Type': 'application/x-www-form-urlencoded'
        },
        success: function (res) {
            //--init data        
            var data = res.data.arr;
            var status = res.data.status;
            if (status != 1) {
                wx.showToast({
                    title: res.data.err,
                    duration: 3000
                });
                return false;
            }
            that.globalData.userInfo['id'] = data.ID;
            that.globalData.userInfo['NickName'] = data.NickName;
            that.globalData.userInfo['HeadUrl'] = data.HeadUrl;
            that.d.userId = data.ID;
            var userId = data.ID;
            if (!userId) {
                wx.showToast({
                    title: '登录失败！',
                    duration: 3000
                });
                return false;
            }
            that.d.userId = userId;
        },
        fail: function (e) {
            wx.showToast({
                title: '网络异常！err:authlogin',
                duration: 2000
            });
        },
    });
},
globalData: {
    userInfo: null
}


})

